//
//  ZClientInfoCenter.m
//  WeClubs
//
//  Created by Zed Link on 30/3/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZClientInfoCenter.h"
#import <sys/utsname.h>

@interface ZClientInfoCenter ()

/** 日期格式器 */
@property (strong, nonatomic) NSDateFormatter *formatter;

@end

@implementation ZClientInfoCenter

Z_SINGLETON_IMPLEMENTATION(ZClientInfoCenter)

#pragma mark - Getter & Setter

- (NSString *)osVersion
{
    return [NSString stringWithFormat:@"%@ %@", [UIDevice currentDevice].systemName, [UIDevice currentDevice].systemVersion];
}

- (NSString *)appVersion
{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

- (NSString *)screenSize
{
    CGSize screen = [UIScreen mainScreen].bounds.size;
    CGFloat scale = [UIScreen mainScreen].scale;
    return [NSString stringWithFormat:@"%.0f, %.0f", screen.width * scale , screen.height *scale];
}

- (long long)timeIntervalSince1970
{
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    long long milliSeconds = interval * 1000;
    
    return milliSeconds;
}

- (NSString *)deviceModel
{
    NSDictionary *rule = @{
                           @"i386" : @"iPhone Simulator 32Bit",
                           @"x86_64" : @"iPhone Simulator 64Bit",
                           @"iPhone3,1" : @"iPhone 4",
                           @"iPhone3,2" : @"iPhone 4",
                           @"iPhone3,3" : @"iPhone 4",
                           @"iPhone4,1" : @"iPhone 4S",
                           @"iPhone4,2" : @"iPhone 4S",
                           @"iPhone4,3" : @"iPhone 4S",
                           @"iPhone5,1" : @"iPhone 5",
                           @"iPhone5,2" : @"iPhone 5",
                           @"iPhone5,3" : @"iPhone 5C",
                           @"iPhone5,4" : @"iPhone 5C",
                           @"iPhone6,1" : @"iPhone 5S",
                           @"iPhone6,2" : @"iPhone 5S",
                           @"iPhone7,1" : @"iPhone 6 PLUS",
                           @"iPhone7,2" : @"iPhone 6",
                           @"iPhone8,1" : @"iPhone 6S",
                           @"iPhone8,2" : @"iPhone 6S PLUS",
                           @"iPhone8,4" : @"iPhone SE",
                           @"iPhone9,1" : @"iPhone 7",
                           @"iPhone9,2" : @"iPhone 7 PLUS",
                           };
    
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceTag = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    __block NSString *model;
    [rule enumerateKeysAndObjectsUsingBlock:^(NSString * key, NSString * obj, BOOL * _Nonnull stop) {
        if ([deviceTag isEqualToString:key]) {
            *stop = YES;
            model = obj;
        }
    }];
    
    return model;
}

- (NSString *)stringByFormatt:(NSString *)format fromMilliSeconds:(long long)milliSeconds
{
    NSTimeInterval interval = (double)milliSeconds / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    // yyyy-MM-dd HH:mm:ss.SSS
    self.formatter.dateFormat = format;
    
    return [self.formatter stringFromDate:date];
}

#pragma mark - Getter & Setter

- (NSDateFormatter *)formatter
{
    if (_formatter == nil) {
        _formatter = [[NSDateFormatter alloc] init];
    }
    return _formatter;
}

@end
